var categoty = document.getElementById("category");
// var fileupload = document.getElementById("fileupload");
var imageview = document.getElementById("imageview");
var xhr;
categoty.onchange = newProductId;
// fileupload.onchange=updateImg;

$("#fileupload").change(function() {
    console.info("www");
    var $file = $(this);
    var objUrl = $file[0].files[0];
    //获得一个http格式的url路径:mozilla(firefox)||webkit or chrome
    var windowURL = window.URL || window.webkitURL;
    //createObjectURL创建一个指向该参数对象(图片)的URL
    var dataURL;
    dataURL = windowURL.createObjectURL(objUrl);
    //把url给图片的src，让其显示
    $("#imageview").attr("src",dataURL);
    $('#imageview').attr("style","display:inline;width: 200px;height: 200px");
});

function newProductId(){
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = processNewId;
    xhr.open("GET","/catalog/createProductId?categoryId="+categoty.value,true);
    xhr.send(null);
}
function processNewId(){
    if(xhr.readyState == 4){
        if(xhr.status == 200){
            var str = xhr.responseText;
            console.info(str);
            var productId = document.getElementById("productId");
            productId.value = str;
        }
    }
}
$(document).ready(function () {

    $("#newProductPage").submit(function () {
        if(category.value == ""){
            console.info("错误");
            alert("请选择宠物类别");
            return false;

        } else if(imageview.getAttribute("src")==""){

            alert("请上传图片");
            return false;
        } else{
            alert("新增产品成功,接下来您可以新增宠物")
            return true;
        }
    })
})